<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.alipay.api.internal.util.AlipaySignature" %>
<%@ page import="com.your_project.config.AlipayConfig" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Iterator" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>支付结果</title>
    <style>
        .result-container {
            text-align: center;
            margin-top: 50px;
            font-family: Arial, sans-serif;
        }
        .success {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }
        .button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            display: inline-block;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="result-container">
        <%
            // 获取支付宝GET过来的信息
            Map<String,String> params = new HashMap<String,String>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = iter.next();
                String[] values = requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                params.put(name, valueStr);
            }

            // 验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(
                params, 
                AlipayConfig.alipay_public_key, 
                AlipayConfig.charset, 
                AlipayConfig.sign_type
            );

            if(signVerified) {
                // 商户订单号
                String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
                // 支付宝交易号
                String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
                
                // 从订单号中提取小说ID和章节ID
                String[] ids = out_trade_no.replace("ORDER", "").split("_");
                String novelId = ids[0];
                String chapterId = ids[1];
        %>
                <div class="success">支付成功！</div>
                <p>订单号: <%= out_trade_no %></p>
                <p>支付宝交易号: <%= trade_no %></p>
                
                <script>
                    // 调用父窗口的支付成功处理函数
                    if (window.opener && !window.opener.closed) {
                        window.opener.onPaymentSuccess('<%= novelId %>', '<%= chapterId %>');
                        setTimeout(function() {
                            window.close();
                        }, 2000);
                    } else {
                        // 如果不是弹窗，则直接跳转
                        setTimeout(function() {
                            window.location.href = 'NovelContentServlet?novel_id=<%= novelId %>&chapter_id=<%= chapterId %>';
                        }, 2000);
                    }
                </script>
                
                <a href="NovelContentServlet?novel_id=<%= novelId %>&chapter_id=<%= chapterId %>" class="button">
                    立即阅读
                </a>
        <%
            } else {
        %>
                <div class="success" style="color: red;">验证失败</div>
                <p>签名验证失败，请联系管理员</p>
        <%
            }
        %>
    </div>
</body>
</html> 